/*******************************************************************************
 * Copyright 2010 Maciej Kaniewski mk@firegnom.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/


package com.firegnom.rat;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Process;

import com.firegnom.rat.util.Constants;

public class DefaultExceptionHandler implements UncaughtExceptionHandler {


  private Context context;
  private Class<? extends ExceptionActivity> a;

  // constructor
  public DefaultExceptionHandler( Context context, Class<? extends ExceptionActivity> a) {
    this.context = context;
    this.a = a;
  }

  // Default exception handler
  public void uncaughtException(Thread t, Throwable e) {
    Intent intent = new Intent(context, a);
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    e.printStackTrace(printWriter);
    try {
      PackageManager pm = context.getPackageManager();
      PackageInfo pi;
      pi = pm.getPackageInfo(context.getPackageName(), 0);
      intent.putExtra(Constants.APPLICATION_VERSION, pi.versionName);
      intent.putExtra(Constants.APPLICATION_PACKAGE, pi.packageName);
      intent.putExtra(Constants.PHONE_MODEL, android.os.Build.MODEL);
      intent.putExtra(Constants.ANDROID_VERSION, android.os.Build.VERSION.RELEASE);
      intent.putExtra(Constants.APPLICATION_STACKTRACE, result.toString());

    } catch (NameNotFoundException ex) {
      ex.printStackTrace();
    }
    context.startActivity(intent);
    Process.killProcess(Process.myPid());
  }
}
